<?php
namespace app\api\controller\revenue_ap;
use think\File;
use think\facade\Env;
use app\common\model\user\User as UserModel;
use app\common\model\company\Company;
use app\common\model\orderdata\Goods;
use app\common\model\quotation\QuotationList;
use app\common\model\rebate\RebateList;
use app\common\model\rebate\Rebate as RebateModel;
use app\api\controller\Base;
use app\api\controller\Baseuser;
use app\common\model\revenue_ap\Rap as RapModel;
use app\common\model\revenue_ap\Raplist as RapListModel;
use app\common\model\cost\Costtype;
use app\common\model\cost\Pay;
use app\common\model\cost\Revenue;
use app\common\model\power\Rolepower;


//挂账日志
class Rap extends Baseuser
{
	//初始化
    public function initialize(){
		// Rolepower::powerguard(101004);
	}

	/**
	 * 增加/修改服务费信息
	 * @param  [type] $type  [description]
	 * @param  [type] $id    [description]
	 * @param  [type] $limit [description]
	 * @param  [type] $note  [description]
	 * @param  [type] $img   [description]
	 * @return [type]        [description]
	 */
	public static function create(){
		$idata = input('post.');
		$sb = RapListModel::get($idata['RAPListId']);
        if(empty($sb)){
        	return myerr('没找到您需要的挂账记录');
        }
        if(empty($idata['Price'])){
        	return myerr('结算为0，无需添加挂账记录');
        }
        if(($idata['Price']+$sb['Price'])>$sb['TotalCost']){
        	return myerr('结算费用超出所需总费用,还需要结算'.bcsub($sb['TotalCost'],$sb['Price'],2).'元。');
        }
        //添加支出/收入表
		$sdata['TypeId'] = Costtype::getid($sb['Type']);
		$sdata['PayTime'] = $idata['SettlementTime'];
		$sdata['RelationUserId'] = '';
		$sdata['RelationListId'] = $sb['CorrelationId'];
		if(!empty(input('Img'))){
			$sdata['UpImg'] = input('Img');
		}
		if(!empty(input('Note'))){
			$sdata['UpNote']   = input('Note');
		}
		if($idata['Price']>0){
		    $sdata['PayCost'] = $idata['Price'];
			Revenue::screate($sdata);
		}else{
			$sdata['PayCost'] = 0 - $idata['Price'];
			Pay::screate($sdata);
		}
        //创建挂账记录
        RapModel::user_create($idata);
        //修改挂账列表信息
        RapListModel::add_price($idata['RAPListId'],$idata['Price'],$idata['ExpectedTime']);
        return myok('登记成功');
	}

	/**
	 * 获取日志详情，修改使用
	 * @return [type] [description]
	 */
	public static function getinfo(){
		$id = input('id');
		$sb = RapModel::getinfo($id);
		return myok($sb);
	}

	/**
	 * 修改服务费档案 relist
	 * @return [type] [description]
	 */
	public function relist(){
		$data = input('post.');
		if(empty($data['id'])){
            return moerr('缺少参数，请重新尝试');
        }
        $list = ['Note','Img','State','IsDelete','Limit'];

        $ndata = [];//需要调整的数据
        foreach ($list as $key => $value) {
        	if(is_set($data[$value])){
        		$ndata[$value] = $data[$value];
        	}
        }
        if(empty($ndata)){
        	return myerr('没有找到需要调整的数据');
        }
        RebateList::user_reinfo($data['id'],$ndata);
		return myok('修改成功');        
	}

	/**
	 * 查询服务费档案 getlist
	 * @return [type] [description]
	 */
	public function getlistinfo(){
		$id = input('id');
		if(empty($id)){
			return myerr('缺少参数，请重新尝试');
		}
		$data = RebateList::getinfo($id);
		if(!$data['code']){
			return myerr($data['msg']);
		}
		$data = $data['msg'];
		return myok($data);
	}


	/**
	 * 记录服务费信息 createinfo 
	 * @return [type] [description]
	 */
	public function add_price(){
		//创建档案
		$data = input('post.');
		$id = input('id');//档案id
		//增加记录信息
		if(!empty($data)){
			RebateModel::user_create($id,$data);
		}
		//修改档案信息 合计服务费 每次服务费后将此值向上增加
		RebateList::add_price($id,$data['Price']);
		return myok();
	}

	/**
	 * 查询服务费记录具体信息 getreinfo
	 * @return [type] [description]
	 */
	public function getreinfo(){
		$id = input('id');
		if(empty($id)){
			return myerr('缺少参数，请重新尝试');
		}
		$data = RebateModel::getinfo($id);
		return myok($data);
	}

	/**
	 * 修改服务费信息 reinfo
	 * @return [type] [description]
	 */
	public function reinfo(){
		$idata  = input('post.');
		if(empty($idata['id'])){
			return myerr('缺少参数，请重新尝试');
		}
        $list = ['Note','Img','IsDelete','ExpectedTime','SettlementTime','Price'];
        $ndata = [];//需要调整的数据
        foreach ($list as $key => $value) {
        	if(isset($idata[$value])){
        		$ndata[$value] = $idata[$value];
        	}
        }
        if(empty($ndata)){
        	return myerr('没有找到需要调整的数据');
        }
        $ndata['id'] = $idata['id'];
        RapModel::user_reinfo($ndata);
		return myok('修改成功');
	}

}
